From 6eb753c5bf67764890856cf23a67c0bf65973c16 Mon Sep 17 00:00:00 2001
From: Maxime Devos <maximedevos@telenet.be>
Date: Thu, 29 Jul 2021 22:24:50 +0200
Subject: [PATCH] Add environment variable MINETEST_MOD_PATH

This adds an environment variable MINETEST_MOD_PATH.
When it exists, Minetest will look there for mods
in addition to ~/.minetest/mods/.

This patch as-is is not yet ready for upstream, because:

  * the GUI will only display mods in MINETEST_MOD_PATH
    or mods in ~/.minetest/mods, it won't combine the two

  * the GUI for installing mods from ContentDB is disabled
    when MINETEST_MOD_PATH is set, because otherwise Minetest
    would try to install mods in the store.

  * MINETEST_MOD_PATH can only have a single component
---
 builtin/mainmenu/dlg_contentstore.lua |  7 +++++++
 src/content/subgames.cpp              |  3 +++
 src/script/lua_api/l_mainmenu.cpp     | 14 +++++++++++++-
 src/script/lua_api/l_mainmenu.h       |  2 ++
 4 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/builtin/mainmenu/dlg_contentstore.lua b/builtin/mainmenu/dlg_contentstore.lua
index 7096c9187..c4a2cbd18 100644
--- a/builtin/mainmenu/dlg_contentstore.lua
+++ b/builtin/mainmenu/dlg_contentstore.lua
@@ -22,6 +22,13 @@ if not core.get_http_api then
 	end
 	return
 end
+if core.mod_path_set() then
+	function create_store_dlg()
+		return messagebox("store",
+				fgettext("Mods from ContentDB cannot be installed when mods from Guix are also installed"))
+	end
+	return
+end
 
 -- Unordered preserves the original order of the ContentDB API,
 -- before the package list is ordered based on installed state.
diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp
index e9dc609b0..1809f189e 100644
--- a/src/content/subgames.cpp
+++ b/src/content/subgames.cpp
@@ -110,6 +110,9 @@ SubgameSpec findSubgame(const std::string &id)
 	std::set<std::string> mods_paths;
 	if (!user_game)
 		mods_paths.insert(share + DIR_DELIM + "mods");
+	const char *env_mod_path = getenv("MINETEST_MOD_PATH");
+	if (env_mod_path)
+		mods_paths.insert(std::string(env_mod_path));
 	if (user != share || user_game)
 		mods_paths.insert(user + DIR_DELIM + "mods");
 
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index ad00de1c4..737550c42 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -495,9 +495,19 @@ int ModApiMainMenu::l_get_user_path(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_modpath(lua_State *L)
 {
+	const char *c_modpath = getenv("MINETEST_MOD_PATH");
 	std::string modpath = fs::RemoveRelativePathComponents(
 		porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
-	lua_pushstring(L, modpath.c_str());
+	if (c_modpath == NULL)
+		c_modpath = modpath.c_str();
+	lua_pushstring(L, c_modpath);
+	return 1;
+}
+
+/******************************************************************************/
+int ModApiMainMenu::l_mod_path_set(lua_State *L)
+{
+	lua_pushboolean(L, NULL != getenv("MINETEST_MOD_PATH"));
 	return 1;
 }
 
@@ -855,6 +865,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
 	API_FCT(get_mapgen_names);
 	API_FCT(get_user_path);
 	API_FCT(get_modpath);
+	API_FCT(mod_path_set);
 	API_FCT(get_clientmodpath);
 	API_FCT(get_gamepath);
 	API_FCT(get_texturepath);
@@ -888,6 +899,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
 	API_FCT(get_mapgen_names);
 	API_FCT(get_user_path);
 	API_FCT(get_modpath);
+	API_FCT(mod_path_set);
 	API_FCT(get_clientmodpath);
 	API_FCT(get_gamepath);
 	API_FCT(get_texturepath);
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index ec2d20da2..719c26077 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -112,6 +112,8 @@ class ModApiMainMenu: public ModApiBase
 
 	static int l_get_modpath(lua_State *L);
 
+	static int l_mod_path_set(lua_State *L);
+
 	static int l_get_clientmodpath(lua_State *L);
 
 	static int l_get_gamepath(lua_State *L);
-- 
2.32.0

